﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Passageways.Modules.Rolodex.Islands.ViewContacts
{
	/// <summary>
	/// Represents the "Manage Categories" module administration page.
	/// </summary>
	public partial class ManageCategories : Page
	{
		/// <summary>
		/// Invoked when the page loads.
		/// </summary>
		protected void Page_Load(object sender, EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				BindCategories();
			}
		}

		/// <summary>
		/// Binds category data to the data grid.
		/// </summary>
		private void BindCategories()
		{
			DataTable dt = new DataTable();
			dt.Columns.Add("ID", typeof(int));
			dt.Columns.Add("Name");
			dt.Columns.Add("Description");
			dt.Columns.Add("ContactCount");
			dt.Columns.Add("LastUpdateBy");
			dt.Columns.Add("LastUpdateDateTime", typeof(DateTime));
			dt.Columns.Add("Administrators");

			foreach(Category c in Category.Get())
			{
				DataRow row = dt.NewRow();

				row["ID"] = c.ID;
				row["Name"] = c.Name;
				row["Description"] = c.Description;
				row["LastUpdateBy"] = c.LastUpdateBy;
				row["LastUpdateDateTime"] = c.LastUpdateDateTime;
				row["ContactCount"] = Contact.GetNumberInCategory(c.ID, null);

				dt.Rows.Add(row);
			}

			GrdActiveCategories.DataSource = dt;
			GrdActiveCategories.DataBind();
		}

		/// <summary>
		/// Displays the new category panel.
		/// </summary>
		private void ShowAddCatetory()
		{
			PnlAddNewCategory.Visible = true;
			PnlEditCategory.Visible = false;

			//TODO: clear the CtrlMessage 
		}

		/// <summary>
		/// Displays the edit category panel
		/// </summary>
		private void ShowEditCategory()
		{
			PnlAddNewCategory.Visible = false;
			PnlEditCategory.Visible = true;

			//TODO: clear the CtrlMessage 
		}

		/// <summary>
		/// Hides the add and edit category panels and only shows the categories grid.
		/// </summary>
		private void HideAddEditCategory()
		{
			PnlAddNewCategory.Visible = false;
			PnlEditCategory.Visible = false;

			BindCategories();
		}

		/// <summary>
		/// Involed when the "Add New Category" toolbar button is clicked.
		/// </summary>
		protected void OnCategoryAddClick(object sender, EventArgs e)
		{
			ShowAddCatetory();
		}

		/// <summary>
		/// Invoked when the "OK" button is clicked in the "Add New Contact Category" panel is clicked.
		/// </summary>
		protected void OnNewCategoryOkClick(object sender, EventArgs e)
		{
			string categoryName = txtCategoryName.Text.Trim();
			
			if(string.IsNullOrEmpty(categoryName))
			{
				//TODO: Show a Warning in CtrlMessage with the text "Category Name is required"
				return;
			}

			if(Category.Exists(categoryName))
			{
				//TODO: Show a Warning in CtrlMessage with the text "Please enter a unique Category Name."
				return;
			}

			Category.Add(categoryName, txtCategoryDescription.Text.Trim());

			//TODO: Show a Success message, "Category Added Successfully", in CtrlMessage 

			txtCategoryName.Text = string.Empty;
			txtCategoryDescription.Text = string.Empty;

			HideAddEditCategory();
		}

		/// <summary>
		/// Invoked when the "Cancel" button is clicked in the "Add New Contact Category" panel is clicked.
		/// </summary>
		protected void OnNewCategoryCancelClick(object sender, EventArgs e)
		{
			HideAddEditCategory();
		}

		/// <summary>
		/// Invoked when an "Edit" button is clicked in the grid.
		/// </summary>
		protected void OnCategoryEditClick(object sender, DataGridCommandEventArgs e)
		{
			int id = (int)GrdActiveCategories.DataKeys[e.Item.ItemIndex];

			ViewState["EditCategoryID"] = id;

			Category c = new Category(id);

			txtEditCategoryDescription.Text = c.Description;
			txtEditCategoryName.Text = c.Name;

			ShowEditCategory();
		}

		/// <summary>
		/// Invoked when the "OK" button is clicked in the "Edit Category" panel is clicked.
		/// </summary>
		protected void OnEditCategoryOkClick(object sender, EventArgs e)
		{
			string name = txtEditCategoryName.Text.Trim();

			if(string.IsNullOrEmpty(name))
			{
				//TODO: Show a Warming in CtrlMessage with the text "Category Name is required"
				return;
			}

			int categoryID = (int)ViewState["EditCategoryID"];

			if(Category.Exists(txtEditCategoryName.Text.Trim()))
			{
				Category existingCategory = Category.Get(txtEditCategoryName.Text.Trim());

				if(existingCategory != null && existingCategory.ID != categoryID)
				{
					//TODO: Show a Warming in CtrlMessage with the text "Please enter a unique Category Name."
					return;
				}
			}

			Category category = new Category(categoryID);
			category.Name = name;
			category.Description = txtEditCategoryDescription.Text.Trim();
			category.Update();

			//TODO: Show a success message, "Category Successfully Edited", in CtrlMessage

			HideAddEditCategory();
		}

		/// <summary>
		/// Invoked when the "Cancel" button is clicked in the "Edit Category" panel is clicked.
		/// </summary>
		protected void OnEditCategoryCancelClick(object sender, EventArgs e)
		{
			HideAddEditCategory();
		}

		/// <summary>
		/// Invoked when the "Delete" button is clicked in the grid.
		/// </summary>
		protected void OnCategoryDeleteClick(object sender, DataGridCommandEventArgs e)
		{
			Category.Remove(GrdActiveCategories.DataKeys[e.Item.ItemIndex].ToString());

			//TODO: Show a success message, "Category Successfully Deleted", in CtrlMessage

			HideAddEditCategory();
		}
	}
}